package com.steven.treatment_nurse.activity;


import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.EditText;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.EMServiceNotReadyException;
import com.steven.treatment_nurse.R;
import com.steven.treatment_nurse.base.BaseActivity;
import com.steven.treatment_nurse.utils.PermissionUtils;


public class MainActivity extends BaseActivity {
    //    private String usernameA = "13213538793";//用户A
//    private String usernameB = "13838838438";//用户B
//    private String pwd = "123456";//密码
    private EditText editUser;
    //定义需要申请的相关权限
    private final String[] PERMISSIONS = new String[]{
            Manifest.permission.CAMERA//相机权限
            , Manifest.permission.RECORD_AUDIO,//获取音频权限
            Manifest.permission.MODIFY_AUDIO_SETTINGS
    };


    @Override
    public int intiLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        editUser = findViewById(R.id.edit_user);
    }

    /**
     * 获取相关权限
     */
    private void getPermission() {
        PermissionUtils.getInstance().checkPermissions(this, PERMISSIONS, permissionsResult);
    }

    //创建监听权限的接口对象
    PermissionUtils.IPermissionsResult permissionsResult = new PermissionUtils.IPermissionsResult() {
        @Override
        public void passPermissons() {
            //当提示申请权限时，全部通过，会走这里（6.0以下直接走这里）
            try {//单参数
                EMClient.getInstance().callManager().makeVideoCall(editUser.getText().toString());
                startActivity(new Intent(MainActivity.this, VideoActivity.class)
                        .putExtra("isComingIn", false)
                        .putExtra("userName", "自己")
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            } catch (EMServiceNotReadyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        public void forbitPermissons() {
            //只要有一个被拒绝，则会走这里，拒绝不在提醒，还是会走这里
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //回调结果
        PermissionUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    //退出登录
    public void logOut(View view) {
        EMClient.getInstance().logout(true);
        EMClient.getInstance().logout(true, new EMCallBack() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgress(int progress, String status) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError(int code, String message) {
                // TODO Auto-generated method stub

            }
        });
    }

    //拨打电话
    public void call(View view) {
        getPermission();
    }
}

